package sanrenxing.dayEnd.backRepertoryTotal.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sanrenxing.dayEnd.backRepertoryTotal.domain.FundModel;
import sanrenxing.dayEnd.backRepertoryTotal.service.FundModelBiz;
import sanrenxing.dayEnd.backRepertoryTotal.service.TransactionService;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 功能:
 * 编写者：
 * 日期：2021
 * 版本：1.0
 */
@RestController
@RequestMapping("/FundModel")
public class FundModelController {
    @Autowired
    private FundModelBiz fundModelBiz;
    //自动装配  业务处理方法 改
    @Autowired
    private TransactionService transactionService;
    @GetMapping("/fund")
    public Map<String, Object> addBalanceSheet(HttpServletRequest req, int page, int limit) {
        //定义多条件查询对象
        FundModel fundModel = new FundModel();
        //接收请求中的交易日期
        String tranDate = req.getParameter("tranDate");
        //判断是否加入
        if (tranDate != null && tranDate.trim().length() != 0) {
            fundModel.setTranDate(tranDate);
        }

        //调用业务层多条件查询方法
        String condition = fundModelBiz.getCount(fundModel);
        //执行多条件查询语句
        List<FundModel> fundModels = fundModelBiz.findByPages(limit, page, condition);
        //获取总记录行数
        int totalRows = fundModelBiz.count(condition);

        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        //证券信息 改
        map.put("securi",transactionService.SecuritiesInfomationLXF());
        map.put("msg", "");
        map.put("data", fundModels);
        map.put("count", totalRows);

        return map;
    }

    @DeleteMapping("/fund/{seName}")
    public String delete(String seName) {
        //调用业务层代码删除数据
        int rows = fundModelBiz.delete(seName);
        return rows > 0 ? "success" : "failure";
    }

    @PutMapping("/batchDel")
    public String batchDel(String seNameStr) {
        //对字符串按","分割
        String[] seNames = seNameStr.split(",");
        //定义返回值
        String result = "delete success";
        //循环删除
        for (String str : seNames) {
            //调用业务层删除记录
            int rows = fundModelBiz.delete(str);
            //判断
            if (rows < 0) {
                result = "delete failure";
                break;
            }
        }
        return result;
    }
}
